/*
 * GrafoPanel.java
 *
 * Created on 19 de Abril de 2009, 13:29
 */

package br.uece.comp.paa.gui;

import java.util.HashMap;

import javax.swing.JPanel;

import br.uece.comp.paa.entity.grafo.Grafo;
import br.uece.comp.paa.entity.grafo.Vertice;
import br.uece.comp.paa.exception.GrafoNuloException;

import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

/**
 * 
 * @author juliano
 */
public class GrafoPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	public static final int DIM = 5;

	public static final int MAX = 600;

	private float maxY;

	private float maxX;

	private float minY;

	private float minX;

	private mxGraph graph;

	private mxGraphComponent graphComponent;

	private HashMap<String, Object> graphicVertices = new HashMap<String, Object>();

	private float fator;

	public void atualizarGrafo(Grafo grafo, Grafo agm)
			throws GrafoNuloException {
		if (grafo == null) {
			throw new GrafoNuloException("Não há grafo para exibição!");
		}
		removeAll();
		maxX = grafo.getMaxX();
		minX = grafo.getMinX();
		maxY = grafo.getMaxY();
		minY = grafo.getMinY();

		fator = maxX-minX;

//		System.out.println("MaxX: " + maxX);
//		System.out.println("MinX: " + minX);
//		System.out.println("MaxY: " + maxY);
//		System.out.println("MinY: " + minY);

		graph = new PAAmxGraph();

		Object parent = graph.getDefaultParent();

		graph.getModel().beginUpdate();
		try {

			// Adicionando os vertices
			Vertice[] vertices = grafo.getVertices();
			for (Vertice v : vertices) {
				Object graphicVertice = graph.insertVertex(parent, v.getId() +
						"", "" , tdX(v.getX()), tdY(v.getY()), DIM, DIM);
				graphicVertices.put(v.getId() + "", graphicVertice);
			}

			// Adicionando as arestas
			Double[][] matrizCusto = grafo.getMatrizCusto();

			for (int i = 0; i < matrizCusto.length; i++) {
				for (int j = i + 1; j < matrizCusto.length; j++) {
					mxCell ed;

					if (matrizCusto[i][j] != null) {

						if (agm == null) {
							//TIRAR O COMENTÁRIO PARA EXIBIR AS ARESTAS.
							/*ed = (mxCell) graph.insertEdge(parent, null, "",
									graphicVertices.get((i + 1) + ""),
									graphicVertices.get((j + 1) + ""));
							ed.setStyle("line;strokeColor=#000000");*/
						} else if (agm.getMatrizCusto()[i][j] != null) {
							ed = (mxCell) graph.insertEdge(parent, null, "",
									graphicVertices.get((i + 1) + ""),
									graphicVertices.get((j + 1) + ""));
							ed.setStyle("line;strokeColor=#FF0000");
						}
					}
				}
			}

		} finally {
			graph.getModel().endUpdate();
		}

		graphComponent = new mxGraphComponent(graph);
		add(graphComponent);
	}
/*
	private int td(float d) {
		float f = ((float)(d+minDistanciaGrafo) / (maxDistanciaGrafo+minDistanciaGrafo)) * MAX;

		return (int) f;
	}*/

	private float tdX(float d) {
		float f =(((d-minX)/(maxX-minX))) * MAX;
//		System.out.println("Valor: " + f + " | d: " + d);
		return f;
	}

	private float tdY(float d) {
		float f =(((d-minY)/(maxY-minY))) * MAX;
//		System.out.println("Valor: " + f + " | d: " + d);
		return f;
	}
}
